<?php
namespace Action;
use HY\Action;
class Daniuwo extends Action{
    public $_user=array();  //当前用户数据
    public $_login=false;   //当前是否登录
    public $_theme;
    public $_uid = -1;
    public $conf;       
    public function __construct(){
        if(isset($GLOBALS['DANIUWO'])) //防止多次执行 构造函数
            return;
        $GLOBALS['DANIUWO'] = true;

        $this->init_conf();
        $this->view = 'Index';
        if(empty($this->conf['debug_page']))
            C("DEBUG_PAGE",false);
        else
            C("DEBUG_PAGE",true);

        define('THEME_NAME',$this->_theme);
        if($this->conf['wwwdomain']){
            define('WWW',$this->conf['wwwdomain'] . '/');
        }else{
            define('WWW','http://'.$_SERVER['HTTP_HOST'] . '/');
        }

        $this->init_user();
        //当前用户组
        define("IS_LOGIN",$this->_login);
        define('NOW_USER',$this->_login?$this->_user['user']:false);
        define("NOW_UID",$this->_uid); //当前用户id
        $this->v('title','页面缺少标题');
        $this->v('tongji',$this->conf['tongji']);

    }
    //初始化配置
    protected function init_conf(){
        $config = S('config')->find('config',['id'=>1]);
        $config = unserialize($config);
        $this->conf = $config;
    }
    //初始化 用户
    protected function init_user(){
        $cookie = cookie("HYBBS_HEX");
        if(!empty($cookie)){
            $UserLib = L("User");
            $user = $UserLib->get_cookie($cookie);

            if(!empty($user)){
                if(isset($user['uid']) 
                    && isset($user['user'])
                    && isset($user['pass'] )
                    ){
                    $user_data = M("User")->read($user['uid']);
                    //更改密码后 重新登录
                    //用户更改用户组后 重新登录
                    if($user_data['pass'] == $user['pass'] && 
                        $user_data['user'] == $user['user'] 
                        ){
                        $this->_uid = $user_data['uid'];
                        $user = $user_data;
                        $this->_user = $user;
                        $this->_login=true;
                        $this->v('user',$this->_user);
                    }
                }
            }
        }
    }
    protected function message($msg,$type=false,$url=''){
        if(IS_AJAX && IS_POST){
            return $this->json(array(
                'error'=>$type,
                'info'=>$msg,
                'url'=>$url
            ));
        }
        $this->v('title',$msg.' - 提示');
        $this->v("msg",$msg);
        $this->v("bool",$type);
        $this->display('message');
    }
}